<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js canvas - lines - colors</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				background-color: #000000;
				margin: 0px;
				overflow: hidden;
			}

			a {
				color:#0078ff;
			}

			#info {
				position: absolute;
				top: 10px; width: 100%;
				color: #ffffff;
				padding: 5px;
				font-family: Monospace;
				font-size: 13px;
				text-align: center;
				z-index:100;
			}

			a {
				color: orange;
				text-decoration: none;
			}

			a:hover {
				color: #0080ff;
			}

		</style>
	</head>
	<body>


		<script src="libs/three.js"></script>

	
		<script src="libs/Detector.js"></script>
		<script src="libs/hilbert2D.js"></script>
		<script src="libs/OrbitControls.js"></script>


		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var mouseX = 0, mouseY = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var camera, scene, renderer;
			var points
			var cubeMap ={}
			var cubeLen 

			var pic_line_units = 64
			var pic_width = 640

			init();
			animate();

			function init() {

				camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 1, 50000 );
				camera.position.x = 0;
				camera.position.z = 0;


				camera.position.y = 1500;

				scene = new THREE.Scene();

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				// lights

				var ambientLight = new THREE.AmbientLight( 0x606060 );
				scene.add( ambientLight );

				var directionalLight = new THREE.DirectionalLight( 0xffffff );
				directionalLight.position.set( 1, 0.75, 0.5 ).normalize();
				scene.add( directionalLight );


				//controls
				var controls = new THREE.OrbitControls( camera, renderer.domElement);
				controls.minDistance = 2;
				controls.maxDistance = 50000;
				controls.maxPolarAngle = Math.PI;

				// 2d lines
				var geometry2 = new THREE.Geometry()
				points = hilbert2D( new THREE.Vector3( 0, 0, 0 ), pic_width/2, 5 )
				var colors2 = []


				for ( var i = 0; i < points.length; i ++ ) {

					geometry2.vertices.push( points[ i ] );

					colors2[ i ] = new THREE.Color( 0xffffff );
					colors2[ i ].setHSL( i / points.length, 1, 0.5 );

				}

				geometry2.colors = colors2;

				
				// lines

				var material = new THREE.LineBasicMaterial( { color: 0xffffff, linewidth: 3, vertexColors: THREE.VertexColors } );

				

				var line2d = new THREE.Line( geometry2, material );
				line2d.position.set( 0, -2, 0 );
				scene.add( line2d );


				

				//fit img into cubes
				fitImgs()

				//

				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
				document.addEventListener( 'touchstart', onDocumentTouchStart, false );
				document.addEventListener( 'touchmove', onDocumentTouchMove, false );

				//

				window.addEventListener( 'resize', onWindowResize, false );



			}

			function onWindowResize() {

				windowHalfX = window.innerWidth / 2;
				windowHalfY = window.innerHeight / 2;

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function onDocumentMouseMove( event ) {

				mouseX = event.clientX - windowHalfX;
				mouseY = event.clientY - windowHalfY;

			}

			function onDocumentTouchStart( event ) {

				if ( event.touches.length > 1 ) {

					event.preventDefault();

					mouseX = event.touches[ 0 ].pageX - windowHalfX;
					mouseY = event.touches[ 0 ].pageY - windowHalfY;

				}

			}

			function onDocumentTouchMove( event ) {

				if ( event.touches.length == 1 ) {

					event.preventDefault();

					mouseX = event.touches[ 0 ].pageX - windowHalfX;
					mouseY = event.touches[ 0 ].pageY - windowHalfY;
				}

			}

			//

			function animate() {
				requestAnimationFrame( animate );
				render();

				for (var i=0;i<points.length-1;i++){
					var point = points[i]

		        	var voxel = cubeMap[point.x+":"+point.z]
		        	if(! voxel){
		        		return
		        	}

		        	var nextPoint = points[i+1]
		        	var nextVoxel = cubeMap[nextPoint.x+":"+nextPoint.z]

		        	voxel.position.x = nextVoxel.position.x
		        	voxel.position.z = nextVoxel.position.z

		        }
		        var lastIndex = points.length-1
	   			var lastPoint = points[lastIndex]
	   			var lastVoxel = cubeMap[lastPoint.x+":"+lastPoint.z]
	   			lastVoxel.position.x = lastVoxel.position.x+cubeLen

				

			}

			function render() {

				

				renderer.render( scene, camera );

			}

			

			function fitImgs(){
				var canvas = document.createElement('canvas');
    			content = canvas.getContext('2d');
    			canvas.width = Math.sqrt(points.length)
    			canvas.height = canvas.width;
				var img = new Image();
			    img.crossOrigin = '*';
			    img.src = "/imgs/mnls.jpeg";
			    img.onload = function() {
			        content.drawImage(img, 0, 0, canvas.width, canvas.height);
			        imgData = content.getImageData(0, 0, canvas.width, canvas.height);
			        //cubes		
			        cubeLen = pic_width/canvas.width
			        for(var i=0;i<canvas.width;i++){//i=row
			        	for(var j=0;j<canvas.height;j++){//j=col
			        		let selectPos = i*canvas.width+j


							var color = (imgData.data[4*selectPos]<<(4*4))+
									(imgData.data[4*selectPos + 1]<<(2*4))+
									imgData.data[4*selectPos + 2]

							var cubeGeo = new THREE.BoxBufferGeometry( cubeLen, 0.1, cubeLen );

							var material= new THREE.MeshPhongMaterial({
								color:color,//三角面颜色
								side:THREE.DoubleSide//两面可见
							});
							var voxel = new THREE.Mesh( cubeGeo, material );
							var posX = (-canvas.width/2+j)*cubeLen+cubeLen/2
							var posY = (-canvas.width/2+i)*cubeLen+cubeLen/2

							voxel.position.set(posX,0,posY)
							scene.add( voxel );

							cubeMap[posX+":"+posY] = voxel



			       		}
			        }
					
			    };
			}


		</script>
	</body>
</html>
